package com.example.doublepink.viewpagertest;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private Adapter mAdapter;
    private boolean left = false;
    private boolean right = false;
    private boolean isScrolling = false;
    private int lastValue = -1;
    private List<View> mListView = new ArrayList<View>();
    private ChangeViewCallback changeViewCallback = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        View view1 = LayoutInflater.from(this).inflate(R.layout.layout1, null);
        View view2 = LayoutInflater.from(this).inflate(R.layout.layout2, null);
        View view3 = LayoutInflater.from(this).inflate(R.layout.layout3, null);
        //viewpager开始添加view
        mListView.add(view1);
        mListView.add(view2);
        mListView.add(view3);
        mAdapter = new Adapter(mListView);
        viewPager.setAdapter(mAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                if (isScrolling){
                    if (lastValue > positionOffsetPixels){
                        right = true;
                        left = false;
                        Log.i("_+++_+_+_","向右");
                    }else if (lastValue < positionOffsetPixels){
                        right = false;
                        left = true;
                        Log.i("_+++_+_+_","向左");
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {

                if(changeViewCallback!=null){
                    changeViewCallback.getCurrentPageIndex(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == 1) {
                    isScrolling = true;
                } else {
                    isScrolling = false;
                }
                if (state == 2) {

                    if(changeViewCallback!=null){
                        changeViewCallback.changeView(left, right);
                    }
                    right = left = false;
                }
            }
        });
    }

    public boolean getMoveRight(){
        return right;
    }
    public boolean getMoveLeft(){
        return left;
    }


    public interface ChangeViewCallback {
        public void changeView(boolean left, boolean right);

        public void getCurrentPageIndex(int index);
    }
    public void  setChangeViewCallback(ChangeViewCallback callback){
        changeViewCallback = callback;
    }

}
